package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Collection;


@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {

    @Autowired
    private FixedAreaService fixedAreaService;

    @Action(value = "fixedArea_save",results = {@Result(name = "success",type = "redirect",location = "./pages/base/fixed_area.html")})
    public String save(){
        fixedAreaService.save(model);
        return SUCCESS;
    }


    @Action(value = "fixedArea_query",results = {@Result(name = "success",type = "json")})
    public String pageQuery(){
        Pageable pageable = new PageRequest(page - 1, rows);
        Specification<FixedArea> specification = new Specification<FixedArea>() {

            @Override
            public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> list = new ArrayList<>();

                //精确查询定区编码
                if (StringUtils.isNotBlank(model.getId())){
                    Predicate p1 = criteriaBuilder.equal(root.get("id").as(String.class), model.getId());
                    list.add(p1);
                }
                //模糊查询所属单位
                if (StringUtils.isNotBlank(model.getCompany())){
                    Predicate p2 = criteriaBuilder.like(root.get("company").as(String.class), "" + model.getCompany() + "%");
                    System.out.println(model.getCompany()+"----"+root.get("company"));
                    list.add(p2);
                }
                //模糊查询分区
                /*if (StringUtils.isNotBlank(model.getSubareas().)){
                    Predicate p3 = criteriaBuilder.like(root.get("company"), "" + model.getCompany() + "%");
                    list.add(p3);
                }*/
                return criteriaBuilder.and(list.toArray(new Predicate[0]));
            }
        };
        Page<FixedArea> page = fixedAreaService.findPageData(specification,pageable);
        pushPageDataToValueStrack(page);
        return SUCCESS;
    }

    // 查询未关联定区列表
    @Action(value = "fixedArea_findNoAssociationCustomers", results = { @Result(name = "success", type = "json") })
    public String findNoAssociationCustomers(){
        // 使用webClient调用 webService接口
        Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
                .accept(MediaType.APPLICATION_JSON)
                .getCollection(Customer.class);

        ActionContext.getContext().getValueStack().push(collection);
        return SUCCESS;
    }

    // 查询关联当前定区的列表
    @Action(value = "fixedArea_findHasAssociationFixedAreaCustomers", results = { @Result(name = "success", type = "json") })
    public String findHasAssociationFixedAreaCustomers(){
        // 使用webClient调用 webService接口
        Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"
                + model.getId())
                .accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
        ActionContext.getContext().getValueStack().push(collection);
        return SUCCESS;
    }

    //关联客户到定区
    private String[] customerIds;
    public void setCustomerIds(String[] customerIds) {
        this.customerIds = customerIds;
    }
    //               fixedArea_associationCourierToFixedArea
    @Action(value = "fixedArea_associationCustomersToFixedArea",results = {@Result(name = "success",type = "redirect",location = "./pages/base/fixed_area.html")})
    public String associationCustomersToFixedArea(){
        //例：http://localhost:9002/crm_management/services/customerService/fixedArea_associationCustomersToFixedArea?customerIdStr=1,3&fixedAreaId=QD001
        String str = StringUtils.join(customerIds, ",");
        WebClient.create("http://localhost:9002/crm_management/services/customerService"
                +"/associationcustomerstofixedarea?customerIdStr="+str+"&fixedAreaId="+model.getId()).put(null);
        return SUCCESS;
    }

    //关联快递员属性驱动
    private Integer courierId;
    private Integer takeTimeId;

    public void setCourierId(Integer courierId) {
        this.courierId = courierId;
    }
    public void setTakeTimeId(Integer takeTimeId) {
        this.takeTimeId = takeTimeId;
    }

    //关联快递员到定区fixedArea_associationCourierToFixedArea
    @Action(value = "fixedArea_associationCourierToFixedArea",results = {@Result(name = "success",type = "redirect",location = "./pages/base/fixed_area.html")})
    public String associationCourierToFixedArea(){
        System.out.println(model);
        fixedAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);

        return SUCCESS;
    }


}
